Learning Objectives

After completing this lesson, you’ll be able to:

Introduction

You arrived early at the office for a meeting, but someone canceled it at the last minute. Typical! Still, it gives you time to carry out an FME project that has been on your mind: a transformer to calculate the average length of linear features.

Resources

1) Open Workspace

Open the starting workspace in FME Workbench (2022.0 or later).

You’ll see that the workspace reads a set of bicycle path data and then does some minor processing to get it into a reasonable state for use in the custom transformer.

You may want to run the workspace to examine the output and see what data we are dealing with, but remember, the custom transformer we create should work on any linear data.

2) Add LengthCalculator

The contents of the transformer will be relatively straightforward, and we’ll start with just two transformers. So, add a LengthCalculator and a StatisticsCalculator transformer to the workspace.

Adding a LengthCalculator and StatisticsCalculator to the Canvas

3) Create Custom Transformer

Select the two newly placed transformers and turn them into a custom transformer called AverageLengthCalculator. Make sure you set Attribute References to Handle With Published Parameters, although at the moment, there aren’t any references to handle.

Filling out the Create Custom Transformer dialog

4) Edit Custom Transformer

Now we have a new custom transformer, let’s tidy it up and make it functional.

Firstly, rename the input port object to Lines (thus communicating what geometry the transformer expects). Then, add an output port object (if you don’t have one already) and rename it to Output. Then connect it to the StatisticsCalculator:Complete port:

New ports

Inspect the StatisticsCalculator parameters. Set the first-row Attribute to _length. Check Mean.

Filling out the StatisticsCalculator

Click OK.

The StatisticsCalculator will now output a variable called _length.mean. Add an AttributeRenamer and rename _length.mean to AverageLength.

Filling out the AttributeRenamer

Click OK.

5) Run Workspace

Run the workspace and inspect the output to ensure everything is working as expected.

Note

Please take note of the AverageLength value; you’ll need it for the quiz.

6) Export Custom Transformer

Now let’s experiment with different transformer modes.

Return to the AverageLengthCalculator tab and select File > Export as Custom Transformer from the menu bar. In the Export as Custom Transformer dialog, make sure you set Insert Mode to Linked by Default. Make sure the Save Location is the default for storing custom transformers (<user>\Documents\FME\Transformers on Windows):

Filling out the dialog to export the custom transformer

Click OK to close the dialog. 

The custom transformer is saved (as AverageLengthCalculator.fmx), and this file will open in a new instance of FME Workbench.

7) Examine Workspace

Go back to the instance of FME Workbench where the original workspace is open. The custom transformer is now a cyan color to denote that it is now a linked transformer (it’s linked because we chose “Linked By Default” ):

Workspace with linked transformer

Right-click the AverageLengthCalculator and choose to Embed the transformer, and then switch back to the linked version. In a real-life scenario, the choice of mode usually depends on whether you are planning to share the transformer.

In embed mode, right-click the transformer and choose Edit. Make any change to the transformer. You’ll find that you can no longer change back to linked mode because the two definitions are now different!

Delete the embedded transformer. FME will prompt you whether you wish to delete the definition too. Click Yes.

Note

It’s important to realize that the definition of an embedded custom transformer can remain in the workspace, even if you don’t use it. If you clicked “No” above, that’s what would have happened. You’ll be able to tell if such a definition remains by looking in the Embedded Transformers section of the transformer gallery.

You’d usually click No (and keep the definition) if you wanted to make use of it later. You’d usually click Yes (and remove the definition) if you decide that the custom transformer is no longer required.

8) Examine Custom Transformer

Select Tools > FME Options on the menu bar. Click the Workbench section and locate and expand the Transformer Options. Check the option to Display Transformer Version:

Checking display transformer version

Using the Quick Add menu or the Transformer Gallery, place a new instance of the AverageLengthCalculator custom transformer in the workspace (it is linked by default, which is fine). If you hover the mouse cursor over the transformer, the pop-up text will show version 1.

Checking the version of a custom transformer in a tooltip

Return to the instance of Workbench where the FMX file is open. Move one of the objects to activate the save button. Then save the file. Notice that FME does not prompt you to save a new version. That’s because FME has not applied versioning yet. There is only one version of the transformer to edit.

So, leaving Workbench open, close the FMX file. Next, click Open and open AverageLengthCalculator.fmx. Now the transformer definition is in a new editing session, FME will prompt you to apply versioning when you save the transformer.

9) Update Custom Transformer

Let’s make an actual update to this transformer rather than just jiggling objects about to prove a point. We could filter data by geometry, so we aren’t trying to measure the length of a point feature or similar.

So, add a GeometryFilter transformer in front of the LengthCalculator. Inspect the parameters, set Mode to Complete, and select Line and Arc (both under Curve Segment) as the Geometries Types to Filter by:

Filter by Arc and Line

Adjust the feature mapping to connect the Line and Arc ports to the LengthCalculator. Add a second output port object by right-clicking on the canvas and selecting Insert Transformer Output. Call the newly placed port <Rejected> and connect the <Unfiltered> data to it, like so:

Adding a <Rejected> port

Now click the Save button to save the custom transformer. FME will prompt you whether you want to create a new version. Click the button labeled New Version to do so:

Save Transformer prompt

You can tell a new version is created by the information at the very top of the Workbench window.

Note

If you weren’t prompted with the Save Transformer dialog box when you saved, this is because you forgot to close the first edit session. Undo your changes back to how the original custom transformer was (without the GeometryFilter and Rejected output port) and save again. This time close the .fmx file and re-open it. When you make your changes, FME will prompt you to create a new version.

10) Update Workspace

Go back to the instance of FME Workbench where the original workspace is open. Click the refresh button on the Transformer Gallery for FME to scan all custom transformers and discover the new version we’ve just created:

Refreshing the Transformer Gallery

Now right-click on the AverageLengthCalculator custom transformer, and there should be an option to Upgrade To Latest Version. Choose this option:

Upgrading a custom transformer to the latest version

The transformer will be refreshed and updated, which you can tell by the presence of a <Rejected> port.

Note

Now that you have this custom transformer, you have various options to share it.

You can put the transformer into a shared folder and then have other users use Tools > FME Options > Default Paths to link their FME to that shared folder.
You can send (e.g., via email) the fmx file to other users and have them install it on their machines. They can install it by double-clicking the file or saving it to their default FME resource folder.

You can publish the transformer to FME Server for you (and others) to use there.